home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.netins.net!isac!gg
- From: gg@isac.hces.com (Greg Goodrich)
- Subject: Re: more problems with qsort
- Message-ID: <1996Feb27.235558.3751@isac.hces.com>
- Organization: Health Care Expert Systems
- X-Newsreader: TIN [version 1.2 PL2]
- References: <177399702S86.JW1675A@american.edu>
- Date: Tue, 27 Feb 1996 23:55:58 GMT
-
- James D. Watson (JW1675A@american.edu) wrote:
- : Hi folks --
- :
- : after reading the FAQ entries about qsort, I thought I had my problem
- : licked, but it seems not.
- :
- : Architecture: SunOS 4.1.3 with ANSI-C compiler.
- :
- : I'm doing some file manipulation and between steps A and B, I need to sort.
- : For various reasons, I don't want to popen() to the sort utility--I want to
- : use qsort. Here's what I'm doing:
- :
- : get number of lines in the file
- : (char**)malloc with enough room for all lines
- : for each line in the file {
- : (char*)malloc(90) /* 90 is enough room for each line in file */
- : copy each line into the newly malloc()ed space
- : point a (char**) to the newly malloc()ed space
- : }
- :
- : so now I should have "lines" number of pointers to pointers to char,
- : each one pointing to 90 bytes containing a line in the file.
- :
- : So, I call qsort(array[0], lines, 90, compare)
- : where compare is my comparison function -- prepared as discussed
- : in the FAQ. Now all I get are core dumps during the call to qsort().
- : :-)
- :
- : The core dump isn't in my compare routine -- it's somewhere else in
- : the qsort() function according to dbx -- so I'm sure it's a problem
- : setting up my data.
-
- I would suggest making sure that you are actually getting the data into
- the malloc'd space the way that you think you are. It sounds like you
- are passing an invalid reference pointer "array[0]" to qsort. I am
- assuming that array[0] is a pointer to char, which is the first input
- line from the file. Make sure that this is actually what is there by
- debugging the program and printing the value before the qsort. Or you
- could place a print in front of the qsort, which accomplishes the same
- thing. If qsort gets passed an invalid ptr as the first argument, it
- will core dump.
-
- Greg.
- --
- _______________________________________
- Greg Goodrich - gg@hces.com
- Software Engineer
- PACE Health Management Systems
-